﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataSync.BLL;
using DataSync.DAL;
using System.Data;

namespace DataSync.Model
{
    public class AccountMonth : SyncBase
    {
        #region CONFIG
        readonly string sConnString = System.Configuration.ConfigurationManager.ConnectionStrings["sConnString"].ConnectionString;
        readonly string tConnString = System.Configuration.ConfigurationManager.ConnectionStrings["tConnString"].ConnectionString;
        readonly string tDBString = System.Configuration.ConfigurationManager.AppSettings["dbName_ACCOUNT_MONTH"]; 
        #endregion

        private bool isSync = false; 

        public AccountMonth()
        {
            base.Scheme = "dbo";
            base.SourceTableName = "ACCOUNT_MONTH";
            base.SourceDateFieldName = "STATISTICS_DATE";
            //base.TargetTableName = "opendatasource('WEB_ETC_ACCOUNT_MONTH','" + tConnString + "').dbo.ACCOUNT_MONTH_";
            base.TargetTableName = tDBString + "." + Scheme + ".ACCOUNT_MONTH_";
            base.TargetDateFieldName = "STAT_DATE";
            base.SyncType = 1;
            base.IsSubTable = true;
        }

        public bool IsSync
        {
            get
            {
                return isSync;
            }
            set
            {
                this.isSync = new Sync().IsSync("SELECT COUNT(1) FROM " + this.SourceTableName + "_RESULT WHERE IS_AMENDED = 1", new DataSync.DAL.SQLHelper());
            }
        }
        
        public bool SyncResult(string syncWhereSql, out Dictionary<string, decimal[]> resultDic, out string resultMsg)
        {
            resultMsg = "提示 : ";
            resultDic = new Dictionary<string, decimal[]>();

            #region CheckSql
            string sourceTableTotalSql = @"SELECT 
            SUM((CASE WHEN PREVIOUS_BALANCE IS NULL THEN 0 ELSE PREVIOUS_BALANCE END)) AS '月汇总账户期初余额',
            SUM((CASE WHEN END_BALANCE IS NULL THEN 0 ELSE END_BALANCE END)) AS '月汇总账户期末余额',
            SUM((CASE WHEN INCOME IS NULL THEN 0 ELSE INCOME END)) AS '月汇总账户通行费收入总额',
            SUM((CASE WHEN PAY IS NULL THEN 0 ELSE PAY END))+
            SUM((CASE WHEN PARKING_PAY IS NULL THEN 0 ELSE PARKING_PAY END)) '月汇总账户通行费消费支出总额(包括停车场)',
            SUM((CASE WHEN REFUND IS NULL THEN 0 ELSE REFUND END)) AS '月汇总账户通行费退费总额', 
            SUM((CASE WHEN REFILL IS NULL THEN 0 ELSE REFILL END)) AS  '月汇总账户通行费回补总额',
            SUM((CASE WHEN SPECIAL_PAY IS NULL THEN 0 ELSE SPECIAL_PAY END)) AS 月汇总账户通行费特殊扣费总额 FROM " + this.SourceTableName + syncWhereSql;

            string targetTableTotalSql = @"SELECT 
            SUM((CASE WHEN PREV_BALANCE IS NULL THEN 0 ELSE PREV_BALANCE END)) AS '月汇总账户期初余额',
            SUM((CASE WHEN BALANCE IS NULL THEN 0 ELSE BALANCE END)) AS '月汇总账户期末余额',
            SUM((CASE WHEN INCOME IS NULL THEN 0 ELSE INCOME END)) AS '月汇总账户通行费收入总额',
            SUM((CASE WHEN PAY IS NULL THEN 0 ELSE PAY END))+
            SUM((CASE WHEN PARKING_PAY IS NULL THEN 0 ELSE PARKING_PAY END)) '月汇总账户通行费消费支出总额(包括停车场)',
            SUM((CASE WHEN REFUND IS NULL THEN 0 ELSE REFUND END)) AS '月汇总账户通行费退费总额', 
            SUM((CASE WHEN REFILL IS NULL THEN 0 ELSE REFILL END)) AS  '月汇总账户通行费回补总额',
            SUM((CASE WHEN SPECIALPAY IS NULL THEN 0 ELSE SPECIALPAY END)) AS 月汇总账户通行费特殊扣费总额 FROM " + this.TargetTableName;
            #endregion

            DataSet sourceDs = new SQLHelper(sConnString).ExecuteSqlDataSet(sourceTableTotalSql);
            DataTable sourceDt = sourceDs.Tables.Count > 0 ? sourceDs.Tables[0] : null;
            DataSet targetDs = new SQLHelper(tConnString).ExecuteSqlDataSet(targetTableTotalSql);
            DataTable targetDt = targetDs.Tables.Count > 0 ? targetDs.Tables[0] : null;

            bool flag = true;

            if (sourceDt != null && targetDt != null && sourceDt.Rows.Count > 0 && targetDt.Rows.Count > 0)
            {
                #region Get resultDic
                resultDic.Add("月汇总账户期初余额", new decimal[] { Convert.ToDecimal(sourceDt.Rows[0]["月汇总账户期初余额"]), Convert.ToDecimal(targetDt.Rows[0]["月汇总账户期初余额"]) });
                resultDic.Add("月汇总账户期末余额", new decimal[] { Convert.ToDecimal(sourceDt.Rows[0]["月汇总账户期末余额"]), Convert.ToDecimal(targetDt.Rows[0]["月汇总账户期末余额"]) });
                resultDic.Add("月汇总账户通行费收入总额", new decimal[] { Convert.ToDecimal(sourceDt.Rows[0]["月汇总账户通行费收入总额"]), Convert.ToDecimal(targetDt.Rows[0]["月汇总账户通行费收入总额"]) });
                resultDic.Add("月汇总账户通行费消费支出总额(包括停车场)", new decimal[] { Convert.ToDecimal(sourceDt.Rows[0]["月汇总账户通行费消费支出总额(包括停车场)"]), Convert.ToDecimal(targetDt.Rows[0]["月汇总账户通行费消费支出总额(包括停车场)"]) });
                resultDic.Add("月汇总账户通行费退费总额", new decimal[] { Convert.ToDecimal(sourceDt.Rows[0]["月汇总账户通行费退费总额"]), Convert.ToDecimal(targetDt.Rows[0]["月汇总账户通行费退费总额"]) });
                resultDic.Add("月汇总账户通行费回补总额", new decimal[] { Convert.ToDecimal(sourceDt.Rows[0]["月汇总账户通行费回补总额"]), Convert.ToDecimal(targetDt.Rows[0]["月汇总账户通行费回补总额"]) });
                resultDic.Add("月汇总账户通行费特殊扣费总额", new decimal[] { Convert.ToDecimal(sourceDt.Rows[0]["月汇总账户通行费特殊扣费总额"]), Convert.ToDecimal(targetDt.Rows[0]["月汇总账户通行费特殊扣费总额"]) });
                #endregion

                #region 月汇总账户期初余额
                if (sourceDt.Rows[0]["月汇总账户期初余额"] != DBNull.Value && sourceDt.Rows[0]["月汇总账户期初余额"] != null && targetDt.Rows[0]["月汇总账户期初余额"] != DBNull.Value && targetDt.Rows[0]["月汇总账户期初余额"] != null)
                {
                    flag = Convert.ToDecimal(sourceDt.Rows[0]["月汇总账户期初余额"]) == Convert.ToDecimal(targetDt.Rows[0]["月汇总账户期初余额"]);
                }
                #endregion

                #region 月汇总账户期末余额
                if (sourceDt.Rows[0]["月汇总账户期末余额"] != DBNull.Value && sourceDt.Rows[0]["月汇总账户期末余额"] != null && targetDt.Rows[0]["月汇总账户期末余额"] != DBNull.Value && targetDt.Rows[0]["月汇总账户期末余额"] != null && flag)
                {
                    flag = Convert.ToDecimal(sourceDt.Rows[0]["月汇总账户期末余额"]) == Convert.ToDecimal(targetDt.Rows[0]["月汇总账户期末余额"]);
                }
                #endregion

                #region 月汇总账户通行费收入总额
                if (sourceDt.Rows[0]["月汇总账户通行费收入总额"] != DBNull.Value && sourceDt.Rows[0]["月汇总账户通行费收入总额"] != null && targetDt.Rows[0]["月汇总账户通行费收入总额"] != DBNull.Value && targetDt.Rows[0]["月汇总账户通行费收入总额"] != null && flag)
                {
                    flag = Convert.ToDecimal(sourceDt.Rows[0]["月汇总账户通行费收入总额"]) == Convert.ToDecimal(targetDt.Rows[0]["月汇总账户通行费收入总额"]);
                }
                #endregion

                #region 月汇总账户通行费消费支出总额(包括停车场)
                if (sourceDt.Rows[0]["月汇总账户通行费消费支出总额(包括停车场)"] != DBNull.Value && sourceDt.Rows[0]["月汇总账户通行费消费支出总额(包括停车场)"] != null && targetDt.Rows[0]["月汇总账户通行费消费支出总额(包括停车场)"] != DBNull.Value && targetDt.Rows[0]["月汇总账户通行费消费支出总额(包括停车场)"] != null && flag)
                {
                    flag = Convert.ToDecimal(sourceDt.Rows[0]["月汇总账户通行费消费支出总额(包括停车场)"]) == Convert.ToDecimal(targetDt.Rows[0]["月汇总账户通行费消费支出总额(包括停车场)"]);
                }
                #endregion

                #region 月汇总账户通行费退费总额
                if (sourceDt.Rows[0]["月汇总账户通行费退费总额"] != DBNull.Value && sourceDt.Rows[0]["月汇总账户通行费退费总额"] != null && targetDt.Rows[0]["月汇总账户通行费退费总额"] != DBNull.Value && targetDt.Rows[0]["月汇总账户通行费退费总额"] != null && flag)
                {
                    flag = Convert.ToDecimal(sourceDt.Rows[0]["月汇总账户通行费退费总额"]) == Convert.ToDecimal(targetDt.Rows[0]["月汇总账户通行费退费总额"]);
                }
                #endregion

                #region 月汇总账户通行费回补总额
                if (sourceDt.Rows[0]["月汇总账户通行费回补总额"] != DBNull.Value && sourceDt.Rows[0]["月汇总账户通行费回补总额"] != null && targetDt.Rows[0]["月汇总账户通行费回补总额"] != DBNull.Value && targetDt.Rows[0]["月汇总账户通行费回补总额"] != null && flag)
                {
                    flag = Convert.ToDecimal(sourceDt.Rows[0]["月汇总账户通行费回补总额"]) == Convert.ToDecimal(targetDt.Rows[0]["月汇总账户通行费回补总额"]);
                }
                #endregion

                #region 月汇总账户通行费特殊扣费总额
                if (sourceDt.Rows[0]["月汇总账户通行费特殊扣费总额"] != DBNull.Value && sourceDt.Rows[0]["月汇总账户通行费特殊扣费总额"] != null && targetDt.Rows[0]["月汇总账户通行费特殊扣费总额"] != DBNull.Value && targetDt.Rows[0]["月汇总账户通行费特殊扣费总额"] != null && flag)
                {
                    flag = Convert.ToDecimal(sourceDt.Rows[0]["月汇总账户通行费特殊扣费总额"]) == Convert.ToDecimal(targetDt.Rows[0]["月汇总账户通行费特殊扣费总额"]);
                }
                #endregion
            }
            else
            {
                flag = false;
                resultMsg = "数据异常，请联系相关人员";
            }
            return flag;
        }
    }
}
